package com.wms.shop.controller;

import java.util.List;

import com.wms.common.core.utils.DateUtils;
import com.wms.common.core.utils.SecurityUtils;
import com.wms.common.core.web.controller.BaseController;
import com.wms.common.core.web.domain.AjaxResult;
import com.wms.common.core.web.page.TableDataInfo;
import com.wms.shop.domain.Cart;
import com.wms.shop.service.ICartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * 购物车Controller
 *
 * @author zzm
 * @date 2021-08-28
 */
@RestController
@RequestMapping("/wx/cart")
public class CartController extends BaseController
{
    @Autowired
    private ICartService cartService;

    /**
     * 查询购物车列表
     */
    @GetMapping("/list")
    public AjaxResult list(Cart cart)
    {
        cart.setUserId(SecurityUtils.getUserId());
        List<Cart> list = cartService.selectCartList(cart);
        return AjaxResult.success(list);
    }


    /**
     * 获取购物车详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(cartService.selectCartById(id));
    }

    /**
     * 获取购物车数量
     */
    @GetMapping(value = "/getCount")
    public AjaxResult getCount()
    {
        Long userId = SecurityUtils.getUserId();
        return AjaxResult.success(cartService.getCountByUserId(userId));
    }

    /**
     * 新增购物车
     */
    @PostMapping(value = "save")
    public AjaxResult add(@RequestBody Cart cart)
    {
        cart.setUserId(SecurityUtils.getUserId());
        return toAjax(cartService.insertCart(cart));
    }

    /**
     * 修改购物车
     */
    @PostMapping(value = "update")
    public AjaxResult edit(@RequestBody Cart cart)
    {
        return toAjax(cartService.updateCart(cart));
    }

    /**
     * 删除购物车
     */
    @PostMapping(value = "delete")
    public AjaxResult remove(@RequestBody String[] ids)
    {
        return toAjax(cartService.deleteCartByIds(ids));
    }

    /**
     * 查询选中购物车规格
     * @param productId
     * @return
     */
    @GetMapping(value = "/selectCartByProductId")
    public AjaxResult selectCartByProductId(Long productId){
        return AjaxResult.success("查询成功!", cartService.selectCartByProductId(productId, SecurityUtils.getUserId()));
    }


}